/**
 * 
 */
package com.czmh.etc.onlinepubsys.utils;

import com.czmh.etc.interpreter.Utils.StringUtils;
import com.czmh.etc.interpreter.entity.base.EnMessagetype;
import com.czmh.etc.interpreter.entity.base.InterpreterContent;
import com.czmh.etc.interpreter.entity.request.*;
import com.czmh.etc.onlinepubsys.utils.httpclient.HttpUtil;
import com.czmh.etc.onlinepubsys.utils.httpclient.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.http.client.ClientProtocolException;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.*;

/**
 * @描述：
 * @author： Seven.
 * @创建日期：Jun 9, 2019 11:31:16 AM.
 * @版本：1.0.0
 */
public class EtcInterpreterUtil {
    
//    private static String baseUrl = "http://172.21.14.129:8016";
    private static String baseUrl = "http://192.168.31.196:8016";
//    private static String baseUrl = "http://localhost:8016";

    private static void testOBUInfoChangeRequest() throws Exception {
        OBUInfoChangeRequest request = new OBUInfoChangeRequest();
        request.setMsgLen(OBUInfoChangeRequest.MSG_LEN);
        request.setMsgType(EnMessagetype.OBUInfoChangeRequest);
        request.setSenderOrgCode(InterpreterContent.SENDER_ORG_CODE);
        request.setRecvOrgCode(InterpreterContent.RECV_ORG_CODE);
        String genTimeStr = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
        request.setVerifyCode(new Long("1838668683"));
        request.setOptType("1");
        request.setSendTime(genTimeStr);
        String sendTimeStr = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
        request.setWasteSn("90" + sendTimeStr + "0713");
        request.setPosID("8000000");
        request.setTerminalID("  100000000001");
        
        request.setGenTime(genTimeStr);
        // OBU卡号
        request.setOBUID("8888021812100144");
        request.setOBUID(StringUtils.rightPadByByteLen("01000110", 20, ' '));
        request.setUserType("0");
        request.setUserName(StringUtils.rightPadByByteLen("陈震霖", 60, ' '));
        request.setCertType("02");
        request.setCertSN(StringUtils.rightPadByByteLen("440921199108205117", 32, ' '));
        request.setWeight(StringUtils.leftPadByByteLen("5", 10, '0'));
        request.setVehWeight(StringUtils.leftPadByByteLen("4525", 5, '0'));
        request.setVehHeight(StringUtils.leftPadByByteLen("1654", 5, '0'));
        request.setVehWidth(StringUtils.leftPadByByteLen("1852", 5, '0'));
        //车轮数   4
        request.setWheelNum("04");
        //车轴数   4
        request.setAxleNum("02");
        //轴距    5
        request.setWheelbase(StringUtils.leftPadByByteLen("2640", 5, '0'));
        
        request.setCardCost("00000200");
        request.setLinkAddr(StringUtils.rightPadByByteLen("联系地址", 100, ' '));
        request.setLinkPost("525357");
        request.setLinkTel(StringUtils.rightPadByByteLen("13760613859", 16, ' '));
        request.setLinkMobile(StringUtils.rightPadByByteLen("13760613859", 16, ' '));
        request.setLinkFax(StringUtils.rightPadByByteLen("", 16, ' '));
        request.setLinkMail(StringUtils.rightPadByByteLen("", 60, ' '));
        request.setVModel(StringUtils.rightPadByByteLen("", 40, ' '));
        request.setVLC("3");
        request.setVLPC("0");
        request.setVLP(StringUtils.rightPadByByteLen("青A23654", 14, ' '));
        request.setVClass("03");
        request.setVopera("1");
        request.setVEngineNum("028937876576376563");
        request.setBrand("02");
        request.setRegisteredType("1");
        request.setModel(StringUtils.rightPadByByteLen("Sophia-V60g", 20, ' '));
        ObjectMapper mapper = new ObjectMapper();
        String jsonStr = mapper.writeValueAsString(request);
        System.out.println(jsonStr);
        String url = baseUrl + "/Interpreter/sendData/"+EnMessagetype.OBUInfoChangeRequest.getMessageType();
        Result result = HttpUtil.httpPost(url, jsonStr);
        System.out.println("请求结果：" + result.getBody());
        
    }

    /**
     * 6.卡信息变更请求     9102
     * @throws Exception
     */
    private static void testCardInfoChangeRequest() throws Exception {
        CardInfoChangeRequest request = new CardInfoChangeRequest();
        request.setMsgLen(CardInfoChangeRequest.MSG_LEN);
        request.setMsgType(EnMessagetype.CardInfoChangeRequest);
        request.setSenderOrgCode(InterpreterContent.SENDER_ORG_CODE);
        request.setRecvOrgCode(InterpreterContent.RECV_ORG_CODE);
        String sendTimeStr = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
        request.setSendTime(sendTimeStr);
        request.setVerifyCode(new Long("1838668683"));
        //63011824220101001983

        request.setWasteSn("90" + sendTimeStr + "0025");
        request.setPosID("9101001");
        request.setTerminalID("100000000001  ");
        request.setGenTime(sendTimeStr);
        request.setOptType('2');
        request.setFileType('1');
        request.setBankID("9101001");
        request.setAccountID(StringUtils.rightPadByByteLen("", 19, ' '));
        request.setCardType("22");
        request.setAccType("232");
        request.setTransType('1');
        request.setTransferLimit(new BigDecimal(10000));
        request.setTallyLimit(new BigDecimal(1000));
//        request.setCardID("63011824220101001983");
        request.setCardID("63011512230104000041");
        request.setUserType('0');
        request.setUserName(StringUtils.rightPadByByteLen("高通测试", 60, ' '));
        request.setCertType("02");
        request.setCertSN(StringUtils.rightPadByByteLen("630102197904070042", 32, ' '));
        request.setCardCost(30);
        request.setLinkAddr(StringUtils.rightPadByByteLen("联系地址", 100, ' '));
        request.setLinkPost("000000");
        request.setLinkTel(StringUtils.rightPadByByteLen("15323318861", 16, ' '));
        request.setLinkMobile(StringUtils.rightPadByByteLen("15323318861", 16, ' '));
        request.setLinkFax(StringUtils.rightPadByByteLen("", 16, ' '));
        request.setLinkMail(StringUtils.rightPadByByteLen("", 60, ' '));
        request.setVLPC('0');
//        request.setVLP(StringUtils.rightPadByByteLen("青A16516", 14, ' '));
        request.setVLP(StringUtils.rightPadByByteLen("青A63362", 14, ' '));
        request.setVclass("03");
        request.setRandom(StringUtils.leftPadByByteLen("23", 8, '0'));
        request.setCardVer("40");
        request.setBrand("02");
        request.setIssuedType('1');
        request.setModel("16531561965168516541");
        List<Object> params = new ArrayList<Object>();
        ObjectMapper mapper = new ObjectMapper();
        String jsonStr = mapper.writeValueAsString(request);
        System.out.println(jsonStr);
        String url = baseUrl + "/Interpreter/sendData/"+EnMessagetype.CardInfoChangeRequest.getMessageType();
        Result result = HttpUtil.httpPost(url, jsonStr);
        System.out.println("请求结果：" + result.getBody());
    }
    
    public static void registorCarInfo(){
        try {
            String url = baseUrl + "/Interpreter/sendData/"+EnMessagetype.CardCarInfoOperateRequest.getMessageType();
            CardCarInfoOperateRequest request = new CardCarInfoOperateRequest();
            request.setMsgLen(ConsumeDetailRequest.MSG_LEN);
            request.setMsgType(EnMessagetype.ConsumeDetailRequest);
            request.setSenderOrgCode("9000000");
            request.setRecvOrgCode("6300000");
            String sendTimeStr = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
            request.setSendTime(sendTimeStr);
            request.setVerifyCode(new Long("1838668683"));

            //流水号 20
            request.setWasteSn("90" + sendTimeStr + "2448");
            //网点编号 7
            request.setPosID("9063176");
            //终端机编号
            request.setTerminalID(StringUtils.rightPadByByteLen("6300204", 14, ' '));

            //申请时间  14
            request.setGenTime(sendTimeStr);
            //操作类型 1
            request.setOptType("1");
            //车牌号 14
            request.setVLP(StringUtils.rightPadByByteLen("青A23654", 14, ' '));
            //车牌底色 1
            request.setVLPC("0");
            //收费车型 2
            request.setVClass("01");
            //机动车所有人 50
            request.setOwnerName(StringUtils.rightPadByByteLen("测试车主", 50, ' '));
            //所有人证件类型 2
            request.setOwnerIdType("02");
            //所有人证件号
            request.setOwnerIdNum(StringUtils.rightPadByByteLen("604145874232116518", 30, ' '));
            //用户类型 1
            request.setUserType("0");
            //注册用户证件类型 2
            request.setCertType("02");
            //注册用户证件号 30
            request.setCertSN(StringUtils.rightPadByByteLen("604145874232116518", 30, ' '));
            //录入方式 1
            request.setRegisteredType("1");
            //所有人联系电话    20
            request.setOwnerTel(StringUtils.rightPadByByteLen("15323318861", 20, ' '));
            //联系地址      100
            request.setAddress(StringUtils.rightPadByByteLen("测试机动车所有人联系地址", 100, ' '));
            //联系人姓名    50
            request.setContact(StringUtils.rightPadByByteLen("联系人", 50, ' '));
            //车辆类型      50
            request.setVehicleType(StringUtils.rightPadByByteLen("行驶证内容bala bala", 50, ' '));
            //行驶证品牌型号  50
            request.setVehicleMode(StringUtils.rightPadByByteLen("行驶证内容，行驶证品牌型号", 50, ' '));
            //车辆使用性质   4
            request.setUseCharacter(StringUtils.leftPadByByteLen("2", 4, '0'));
            //车辆识别代号   50
            request.setVIN(StringUtils.rightPadByByteLen("车辆识别代号，行驶证内容", 50, ' '));
            //车辆发动机号    50
            request.setEngineNum(StringUtils.rightPadByByteLen("E1561561", 50, ' '));
            //注册日期    20
            request.setRegisterDate(StringUtils.rightPadByByteLen("20111120", 20, ' '));
            //发证日期   20
            request.setIssueDate(StringUtils.rightPadByByteLen("20111120", 20, ' '));
            //档案编号   50
            request.setFileNum(StringUtils.rightPadByByteLen("51asd65f1a6s5d1f", 50, ' '));
            //定载人数   4
            request.setApprovedCount(StringUtils.leftPadByByteLen("7", 4, '0'));
            //总质量    10
            request.setTotalMass(StringUtils.leftPadByByteLen("1500", 10, '0'));
            //整备质量   10
            request.setMaintenanceMass(StringUtils.leftPadByByteLen("1500", 10, '0'));
            //核定载质量  10
            request.setPermittedWeight(StringUtils.leftPadByByteLen("1500", 10, '0'));
            //外廓尺寸   100
            request.setOutsideDimensions(StringUtils.rightPadByByteLen("1500×1500×1300", 100, '0'));
            //牵引总质量  10
            request.setPermittedToWeight(StringUtils.leftPadByByteLen("1500", 10, '0'));
            //检验记录    50
            request.setTestRecord(StringUtils.rightPadByByteLen("testrecords", 50, ' '));
            //车路数   4
            request.setWheelCount(StringUtils.leftPadByByteLen("4", 4, '0'));
            //车轴数   4
            request.setAxleCount(StringUtils.leftPadByByteLen("2", 4, '0'));
            //轴距    10
            request.setAxleDistance(StringUtils.leftPadByByteLen("1500", 10, '0'));
            //轴型    50
            request.setAxisType(StringUtils.rightPadByByteLen("轴型dlknfadnb", 50, ' '));

            ObjectMapper mapper = new ObjectMapper();
            String jsonStr = mapper.writeValueAsString(request);
            System.out.println(jsonStr);
            Result result = HttpUtil.httpPost(url, jsonStr);
            System.out.println("请求结果：" + result.getBody());
        }catch (Exception exp){
            exp.printStackTrace();
        }
    }
    
    /**
     * 青通卡信息查询   9127
     * @throws ClientProtocolException
     * @throws IOException
     */
    private static void testCardInfoRequest() throws ClientProtocolException, IOException {
        String url = baseUrl + "/Interpreter/sendData/" + EnMessagetype.CardInfoRequest.getMessageType();
        Map<String, String> params = new HashMap<String, String>();
        CardInfoRequest request = new CardInfoRequest();
        //00000168910791000006300000201606140839026D97DB8B912016061408390215999101001100000000001  630100000000100543402630102197904070042              000000610020160614083902

        request.setMsgLen(CardInfoRequest.MSG_LEN);
        request.setMsgType(EnMessagetype.CardInfoRequest);
        request.setSenderOrgCode(InterpreterContent.SENDER_ORG_CODE);
        request.setRecvOrgCode(InterpreterContent.RECV_ORG_CODE);
        String sendTimeStr = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
        request.setSendTime(sendTimeStr);
        request.setVerifyCode(new Long("1838668683"));

        request.setWasteSn("91" + sendTimeStr + "1599");
        //request.setWasteSn("80" + sendTimeStr + "0713");
        request.setPosID("9101001");
        request.setTerminalID("100000000001  ");
        //request.setCardNo("63010000000010054340");
        request.setCardNo("63011824220101001983");
        request.setCertType("02");
        request.setCertSN("630102197904070042              ");
        request.setCardStoreMoney(new Long("0000006100"));
        request.setQueryTime(sendTimeStr);
        ObjectMapper mapper = new ObjectMapper();
        String jsonStr = mapper.writeValueAsString(request);
        
        Result result = HttpUtil.httpPost(url, jsonStr);
        
        
        System.out.println(result.getBody());
    }

    /**
     * 1.青通联名卡用户信息注册  9133
     * @throws UnsupportedEncodingException
     * @throws ClientProtocolException
     * @throws IOException
     */
    private static void register() throws UnsupportedEncodingException, ClientProtocolException, IOException {
        String url = baseUrl + "/Interpreter/sendData/" + EnMessagetype.CardUserInfoOperateRequest.getMessageType();
        CardUserInfoOperateRequest request = new CardUserInfoOperateRequest();
        request.setMsgLen(CardUserInfoOperateRequest.MSG_LEN);
        request.setMsgType(EnMessagetype.CardUserInfoOperateRequest);
        request.setSenderOrgCode(InterpreterContent.SENDER_ORG_CODE);
        request.setRecvOrgCode(InterpreterContent.RECV_ORG_CODE);
        String sendTimeStr = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
        request.setSendTime(sendTimeStr);
        request.setVerifyCode(new Long("2518611137"));

        request.setWasteSn("90201906101650490713");
//        request.setWasteSn("90" + sendTimeStr + "0713");
        request.setPosID("6000001");
        request.setTerminalID("  100000000001");
        request.setGenTime(sendTimeStr);
        request.setOptType("1");
        request.setUserType("0");
        request.setUserName(StringUtils.rightPadByByteLen(InterpreterContent.USER_NAME, 60, ' '));
        request.setCertType(InterpreterContent.USER_CERT_TYPE);
        request.setCertSN(StringUtils.rightPadByByteLen(InterpreterContent.USER_CERT_SN, 32, ' '));
        request.setLinkAddr(StringUtils.rightPadByByteLen("广东省信宜市朱砂镇", 100, ' '));
        request.setDepartment(StringUtils.rightPadByByteLen("", 100, ' '));
        request.setRegisteredType("1");
        request.setAgentName(StringUtils.rightPadByByteLen("", 150, ' '));
        request.setAgentIdType(StringUtils.rightPadByByteLen("", 2, ' '));
        request.setAgentIdNum(StringUtils.rightPadByByteLen("", 30, ' '));
        request.setTel(StringUtils.rightPadByByteLen("", 20, ' '));
        request.setBank(StringUtils.rightPadByByteLen("", 300, ' '));
        request.setBankAddr(StringUtils.rightPadByByteLen("", 300, ' '));
        request.setBankAccount(StringUtils.rightPadByByteLen("", 20, ' '));
        request.setTaxpayerCode(StringUtils.rightPadByByteLen("", 20, ' '));
        
        ObjectMapper mapper = new ObjectMapper();
        String jsonStr = mapper.writeValueAsString(request);
        System.out.println(jsonStr);
        Result result = HttpUtil.httpPost(url, jsonStr);
        System.out.println(result.getBody());
    }
    
    public static void testOBUAction() throws Exception {
        String url = baseUrl + "/Interpreter/sendData/" + EnMessagetype.OBUActivationRequest.getMessageType();
        OBUActivationRequest request = new OBUActivationRequest();
        request.setMsgLen(OBUActivationRequest.MSG_LEN);
        request.setMsgType(EnMessagetype.OBUActivationRequest);
        request.setSenderOrgCode(InterpreterContent.SENDER_ORG_CODE);
        request.setRecvOrgCode(InterpreterContent.RECV_ORG_CODE);
        String sendTimeStr = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
        request.setSendTime(sendTimeStr);
        request.setVerifyCode(new Long("2518611137"));
//        request.setWasteSn("90201906101650490713");
        request.setWasteSn("90" + sendTimeStr + "0713");
        request.setPosID("6000001");
        request.setTerminalID("  100000000001");
        
        
        
        request.setOperType("2");
        request.setFileType("1");
        request.setOBUID("1122334455667788");
        request.setVLP(StringUtils.rightPadByByteLen("青A01983", 14, ' '));
        request.setVLPC("0");
        request.setVType("00");
        request.setRandom("0bc08786");
        String genTimeStr = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
        request.setGenTime(genTimeStr);

        ObjectMapper mapper = new ObjectMapper();
        String jsonStr = mapper.writeValueAsString(request);
        System.out.println(jsonStr);
        Result result = HttpUtil.httpPost(url, jsonStr);
        System.out.println(result.getBody());
    }
    
    public static void testVehicleCheckRequest() throws Exception {
        String url = baseUrl + "/Interpreter/sendData/" + EnMessagetype.VehicleCheckRequest.getMessageType();
        VehicleCheckRequest request = new VehicleCheckRequest();
        request.setMsgLen(OBUActivationRequest.MSG_LEN);
        request.setMsgType(EnMessagetype.VehicleCheckRequest);
        request.setSenderOrgCode(InterpreterContent.SENDER_ORG_CODE);
        request.setRecvOrgCode(InterpreterContent.RECV_ORG_CODE);
        String sendTimeStr = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
        request.setSendTime(sendTimeStr);
        request.setVerifyCode(new Long("2518611137"));
//        request.setWasteSn("90201906101650490713");
        request.setWasteSn("90" + sendTimeStr + "0713");
        request.setPosID("6000001");
        request.setTerminalID("  100000000001");
        
        request.setVLP(StringUtils.rightPadByByteLen("青A63362", 14, ' '));
        request.setVLPC("0");
        request.setVType("01");
        String genTimeStr = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
        request.setGenTime(genTimeStr);
        
        ObjectMapper mapper = new ObjectMapper();
        String jsonStr = mapper.writeValueAsString(request);
        System.out.println(jsonStr);
        Result result = HttpUtil.httpPost(url, jsonStr);
        System.out.println(result.getBody());
    }
    
    
    public static void main(String[] args) throws Exception {
        // 1.青通联名卡用户信息注册  9133
//        register();
       // 2.青通联名卡车辆信息注册   9134
//        registorCarInfo();
        
        // 3.青通卡用户信息查询   9127
//        testCustomBindingInfoRequest();
//        testCardInfoRequest();
        
//         6.青通联名卡发行     9102
        testCardInfoChangeRequest();
        
        // 7.OBU信息变更  9104
        testOBUInfoChangeRequest();
        
        // 激活OBU
        testOBUAction();
        
        // 车牌校验申请报文(9138)
//        testVehicleCheckRequest();
//        String str = hexStringToString("c7e041303139383300000000");
//        System.out.println(str);
    }

    /**
     * 16进制转换成为string类型字符串
     * @param s
     * @return
     */
    public static String hexStringToString(String s) {
        if (s == null || "".equals(s)) {
            return null;
        }
        s = s.replace(" ", "");
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "GBK");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }

    private static void testCustomBindingInfoRequest() throws Exception {
      String url = baseUrl + "/Interpreter/sendData/"+EnMessagetype.CustomBindingInfoRequest.getMessageType();
      CustomBindingInfoRequest request = new CustomBindingInfoRequest();
      request.setMsgLen(OBUActivationRequest.MSG_LEN);
      request.setMsgType(EnMessagetype.CustomBindingInfoRequest);
      request.setSenderOrgCode(InterpreterContent.SENDER_ORG_CODE);
      request.setRecvOrgCode(InterpreterContent.RECV_ORG_CODE);
      String sendTimeStr = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
      request.setSendTime(sendTimeStr);
      request.setVerifyCode(new Long("2518611137"));
      request.setWasteSn("90" + sendTimeStr + "0713");
      request.setPosID("6000001");
      request.setTerminalID("  100000000001");
      
      ///
      byte type = 1;
      byte vtype = 0;
      request.setCardNo(StringUtils.leftPadByByteLen("621823746609234634", 20, '0'));
      request.setCardType(type);
      request.setCertType(type);
      request.setCertSN(StringUtils.rightPadByByteLen(InterpreterContent.USER_CERT_SN, 32, ' '));
      request.setVLP(StringUtils.rightPadByByteLen("青A01983", 14, ' '));
      request.setVLPC(vtype);
      request.setQueryTime(DateFormatUtils.format(new Date(), "yyyyMMddHHmmss"));
      
      ObjectMapper mapper = new ObjectMapper();
      String jsonStr = mapper.writeValueAsString(request);
      System.out.println(jsonStr);
      Result result = HttpUtil.httpPost(url, jsonStr);
      System.out.println(result.getBody());
    }
}
